/*
============================================================================
 Name        : FlashBoxServerSession.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CFlashBoxServerSession implementation
============================================================================
*/

#include "FlashBoxServerSession.h"

_LIT( KFlashBoxServerName,"FlashBoxServer" ); // Server name
_LIT( KFlashBoxSemaphoreName, "FlashBoxServerSemaphore" );


// FUNCTION PROTOTYPES
static TInt StartServer();
static TInt CreateServerProcess();

RFlashBoxServerSession::RFlashBoxServerSession()
: RSessionBase()
{
	// No implementation required
}


// -----------------------------------------------------------------------------
// RFlashBoxServerSession::Connect()
// Connects to the server and create a session.
// -----------------------------------------------------------------------------
//
TInt RFlashBoxServerSession::Connect()
    {
    TInt error = ::StartServer();

    if ( KErrNone == error )
        {
        error = CreateSession( KFlashBoxServerName,
                               Version(),
                               KDefaultMessageSlots );
        }
    return error;
    }

// -----------------------------------------------------------------------------
// RFlashBoxServerSession::Version()
// Gets the version number.
// -----------------------------------------------------------------------------
//
TVersion RFlashBoxServerSession::Version() const
    {
    return( TVersion( KFlashBoxMajorVersionNumber,
                      KFlashBoxMinorVersionNumber,
                      KFlashBoxBuildVersionNumber ) );
    }

// -----------------------------------------------------------------------------
// RFlashBoxServerSession::RequestSetting()
// Issues a request for the time to the server.
// -----------------------------------------------------------------------------
//
void RFlashBoxServerSession::RequestSetting( const TFlashBoxServRqst& aRqst, TUint8& aStatus) const
    {
    // Create descriptor to enable copying data between client and server.
    // Note: This can be local since this is a synchronous call.
    // Note : Using TPtr8 since this is binary information.
    TPtr8 descriptor( reinterpret_cast<TUint8*>( &aStatus ), sizeof( aStatus ),
                      sizeof( aStatus ) );
    
    // Package message arguments before sending to the server
    TIpcArgs args( &descriptor );
        
    // This call waits for the server to complete the request before
    // proceeding. 
    SendReceive( aRqst, args );
    }


// ============================= OTHER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// StartServer()
// Starts the server if it is not already running
// -----------------------------------------------------------------------------
//
static TInt StartServer()
    {
    TInt result;

    TFindServer findBoxServer( KFlashBoxServerName );
    TFullName name;

    result = findBoxServer.Next( name );
    if ( result == KErrNone )
        {
        // Server already running
        return KErrNone;
        }

    RSemaphore semaphore;
    result = semaphore.CreateGlobal( KFlashBoxSemaphoreName, 0 );
    if ( result != KErrNone )
        {
        return  result;
        }

    result = CreateServerProcess();
    if ( result != KErrNone )
        {
        return  result;
        }

    semaphore.Wait();
    semaphore.Close();

    return KErrNone;
    }

// -----------------------------------------------------------------------------
// CreateServerProcess()
// Creates a server process
// -----------------------------------------------------------------------------
//
static TInt CreateServerProcess()
    {
    TInt result;

    const TUidType serverUid( KNullUid, KNullUid, KServerUid3 );

    RProcess server;
    
    result = server.Create( KFlashBoxServerFilename, KNullDesC, serverUid );
	if ( result != KErrNone )
        {
        return  result;
        }

    server.Resume();
    server.Close();

    return  KErrNone;
    }


// End of File
